package cn.com.tuns.assess.qsb.frame.util;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.channels.FileChannel;

public class FileUtil {

    public static boolean copyFile(String fromPath, String toPath) {
        return copyFile(new File(fromPath), new File(toPath));
    }

    public static boolean copyFile(File from, File to) {
        if (from.exists()) {
            FileChannel inChannel = null, outChannel = null;
            try {
                inChannel = new FileInputStream(from).getChannel();
                outChannel = new FileOutputStream(to).getChannel();
                inChannel.transferTo(0, inChannel.size(), outChannel);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (inChannel != null || outChannel != null) {
                    try {
                        inChannel.close();
                        outChannel.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }

    public static String getContentFromFile(String path) {
        return getContentFromFile(path,"utf-8");
    }

    /**
     * 将文件转化为字符串
     *
     * @author Jacen
     * @email jacen@wscnydx.com
     * @datetime @2015-11-11下午7:56:20
     */
    public static String getContentFromFile(String path,String code) {
        String res = "";
        try {
            FileInputStream is = new FileInputStream(path);
            int length = is.available();
            byte[] buffer = new byte[length];
            is.read(buffer);
            res = new String(buffer, code);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String getContentFromAssert(Context context, String fileName){
        return getContentFromAssert(context,fileName,"utf-8");
    }

    public static String getContentFromAssert(Context context,String fileName, String code)
    {
        try {
            InputStream in = context.getResources().getAssets().open(fileName);
            return getContentFromStream(in,code);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    public static String getContentFromStream(InputStream in)
    {
        return getContentFromStream(in,"utf-8");
    }

    public static String getContentFromStream(InputStream in,String code)
    {
        String result = "";
        try {
            int lenght = in.available();
            //创建byte数组
            byte[]  buffer = new byte[lenght];
            //将文件中的数据读到byte数组中
            in.read(buffer);
            result = new String(buffer,code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                in.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }


}
